package com.eaoa.uc.bustransfer.bo;

import java.util.List;

import com.eaoa.base.EABaseBusinessObject;
import com.eaoa.constants.BusOrderConstants;
import com.eaoa.constants.PubBillConstants;
import com.eaoa.dao.bus.BusOrderDAO;
import com.eaoa.query.bus.BusOrderQuery;
import com.eaoa.vo.bus.BusOrder;
import com.eaoa.vo.bus.BusTransfer;
import com.stony.core.exception.RunException;
import com.stony.core.keygenerator.FillCodeBO;

public class BusTransferAddOrdersBO extends EABaseBusinessObject{

	private static BusOrderDAO orderDAO = new BusOrderDAO();
//	private static BusTransferDAO transferDAO = new BusTransferDAO();
	private List ordernos ;
	
	public BusTransferAddOrdersBO( List ordernos) {
		super();
		this.ordernos=ordernos;
	}
	
	protected void performBusinessLogic() {
		BusOrderQuery orderQuery = new BusOrderQuery();
		orderQuery.setOrderno(ordernos);
		orderQuery.setIsTransfer("N");
		orderQuery.setBillstatus(BusOrderConstants.STATUS_RUN);
		orderQuery.execute();
		
		List orders =  orderQuery.getResults();
		
		if(orders.size()<=0){
			throw new RunException("busOrder.error.create_transfer");
		}else{
			BusTransfer transfer = new BusTransfer();
			FillCodeBO codeBO = new FillCodeBO(PubBillConstants.BILLCODE_TRANSFER);
			codeBO.execute();
			transfer.setBillcode(codeBO.getNewCode());
			BusTransferModifyBO transferModifyBO =new  BusTransferModifyBO(transfer);
			transferModifyBO.execute();
			
			Long transferno = transferModifyBO.getTransferno();
			transfer.setTransferno(transferno);
			
			for(int i=0; i<orders.size(); i++){
				BusOrder order = (BusOrder)orders.get(i);
				order.setBusTransfer(transfer);
				order.setIstransfer("Y");
			}
			orderDAO.saveOrUpdateBatch(orders);
		}

	}
}
